//
//  YZRouter+AutoVC.m
//  VET
//
//  Created by Lam BG on 2018/7/14.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZRouter+AutoVC.h"

#import "YZAutoVCModel.h"
#import "YZAutoRowModel.h"

#import "YZAutoCache.h"
#import "YZAutoFormHeader.h"
#import "YZJsonFileLoader.h"

#import "MJExtension.h"

@implementation YZRouter (AutoVC)

- (void)yz_atuoPush:(NSString*)vcKey {
    
    [self yz_atuoPush:vcKey params:nil];
}

- (void)yz_atuoPush:(NSString*)vcKey params:(NSDictionary*)params {
    
    [self yz_atuoPush:vcKey params:params itemClick:nil];
}

- (void)yz_atuoPush:(NSString*)vcKey params:(NSDictionary*)params itemClick:(itemClick)itemClick {
    
    UIViewController *vc = [self vc:vcKey];
    if (params.count) {
        
        for (NSString *key in params.keyEnumerator) {
            
            [vc setValue:params[key] forKey:key];
        }
    }
    
    // 设置回调
    if (itemClick) {
        
        [vc setValue:itemClick forKey:@"itemClick"];
    }
    
    [[self currentViewController].navigationController pushViewController:vc animated:YES];
}


- (UIViewController*)vc:(NSString*)vcKey {
    
    NSAssert(vcKey.length > 0, @"数据出错 请检查 AutoVC json 文件 %@ 字段", vcKey);
    
    // 获取 vc 数据
    NSDictionary *data = [YZJsonFileLoader loadLocalJsonWithFileName:@"autoVC"];
    YZAutoVCModel *avcModel = [YZAutoVCModel mj_objectWithKeyValues:data[vcKey]];
    
    UIViewController *vc = [[NSClassFromString(avcModel.vcName) alloc] initWithNibName:avcModel.vcName bundle:nil];
    [vc setValue:avcModel forKey:@"avcModel"];
    
    return vc;
}

@end









